<?php
namespace Tyrone\Command;
use Tyrone\Model\Game\PayLog;
use Tyrone\Model\Game\Role;
use Swoole\Coroutine;
use Tyrone\Model\PlayerPay;

class RolePayStatistics extends Base
{

    public function divideHandle($mysql,$platform){
        $currentTimestamp = time();
        $date = date("Ymd", strtotime($this->sDate));

        $this->setFrequency(300);
        $defaultWhere = [
            'p.platform' => $platform,
            //'p.plat_server' => ,
        ];

        $PlayerPayModel = new PlayerPay($mysql);
        echo 'divideHandle-----Tyrone\Command\RolePayStatistics:'.$platform.'_'.PHP_EOL;
        //当前在线数据
        $payment = $PlayerPayModel->statPay($defaultWhere + [
                'pay_date'=>$date
            ]);
        echo 'divideHandle-----Tyrone\Command\RolePayStatistics2:'.$platform.'_'.PHP_EOL;
        $payMonthData = $PlayerPayModel->statPay($defaultWhere + [
                'pay_date[<>]' => [date("Ym01", strtotime($this->sDate)),$date],
            ]);
        echo 'divideHandle-----Tyrone\Command\RolePayStatistics3:'.$platform.'_'.PHP_EOL;
        //插入
        $this->insertDayOnlineDataByCenter($mysql,$date,$payment);
        echo 'divideHandle-----Tyrone\Command\RolePayStatistics4:'.$platform.'_'.PHP_EOL;
        $this->insertDayOnlineDataByCenter($mysql,$date,$payMonthData,[7=>'rmb']);
        echo 'divideHandle-----Tyrone\Command\RolePayStatistics5:'.$platform.'_'.PHP_EOL;
        return [count($payment),count($payMonthData)];
    }

    public  function execute()
    {

        return $this->divideCenter();
    }

    public function insertDayOnlineDataByCenter($mysql,$date,$datass,$projectEenm=[]){
        $DayOnline = new \Tyrone\Model\DayOnline($mysql);
        $day = date("Ymd", strtotime($date));
        !$projectEenm && $projectEenm = [2 => 'rmb',3=>'num',4=>'players'];

        foreach (array_chunk($datass,1000) as $datas){
            foreach ($datas as $item) {
                $temItem = $item;
                foreach ($projectEenm as $project => $field) {
                    $temItem = ['day' => $day, 'stat_project' => $project, 'type' => 0] + $temItem;
                    $other = ['master_platform' => $temItem['platform']];
                    $DayOnline->insertData($temItem, $this->minus, $field, $other);
                }
            }
            usleep(100);

        }
    }
}